package net.zoneland.knowledge.aop;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 请求拦截
 *
 * @author zoux
 */
@Configuration
public class ReqInterceptor implements WebMvcConfigurer {


    /**
     * 忽略拦截的接口
     */
    private static final String[] EXCLUDE_PATHS = new String[]{
        "/swagger-resources",
        "/swagger-resources/**",
        "/webjars/**",
        "/v2/**",
        "/v3/**",
        "swagger-ui.html",
        "/swagger-ui.html/**",
        "/swagger-ui/**",
        "index.html",
        "/api",
        "/api-docs",
        "/api-docs/**",
        "/error",
        // 健康检查页面
        "/*/health",
        "/favicon.ico",
        "/apiToken/**",
        "/",
        "/csrf",
        "/csrf",
    };

    /**
     * 创建请求接口权限拦截器的bean
     * @return 权限拦截器
     */
    @Bean
    public AuthorityInterceptor initAuthorityInterceptor() {
        return new AuthorityInterceptor();
    }

    /**
     * 添加请求拦截规则
     * @param registry 注册
     */
    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(initAuthorityInterceptor())
                .addPathPatterns("/**")
//                .addPathPatterns("/*/ua/getUserByTime")
//                .addPathPatterns("/ua/getOrgByTime/**")
                // 初始化缓存、查询缓存key不需要拦截
                .excludePathPatterns(EXCLUDE_PATHS);
    }
}
